package pt.processingQueues.principal; import java.util.ArrayList; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.logging.Level; import java.util.logging.Logger; /** * Takes clients generated by the ClientGenerator and puts them in a Queue(SupermarketCheckout). * @author Chiti * */ public class Scheduler implements Runnable{ private static final Logger LOGGER = Logger.getLogger(Scheduler.class.getName()); private ArrayList<SupermarketCheckout> queues=new ArrayList<SupermarketCheckout>(); private BlockingQueue<Client> clients=new ArrayBlockingQueue<Client>(100); private boolean isRunning; public Scheduler(){ this.isRunning=true; } @Override public void run() { LOGGER.info("Logger Name: "+LOGGER.getName()); while (true) { while (isRunning) { if (queues.size() > 0 && clients.size() > 0) { SupermarketCheckout queue = getMin(); Client client=new Client(); try { client = clients.take(); } catch (InterruptedException e) { LOGGER.log(Level.SEVERE, "Cannot take a client from queue", e); } queue.addClient(client); } } } } /** * adds a client from the clientGenerator * @param client */ public void addClient(Client client){ try { this.clients.put(client); } catch (InterruptedException e) { LOGGER.log(Level.SEVERE, "Cannot add a client to schedulers queue", e); } } private SupermarketCheckout getMin(){ int min = queues.get(0).getClients().size(); SupermarketCheckout minQueue = queues.get(0); for (SupermarketCheckout queue : queues) { if (queue.getClients().size()< min) { min = queue.getClients().size(); minQueue=queue; } } return minQueue; } /** * adds a checkout in the scheduler * @param queue */ public void addSupermarketCheckout(SupermarketCheckout queue){ System.out.println(queue.toString()+" was added!"); this.queues.add(queue); } /** * @return the queues */ public ArrayList<SupermarketCheckout> getQueues() { return queues; } /** * @param queues the queues to set */ public void setQueues(ArrayList<SupermarketCheckout> queues) { this.queues = queues; } /** * @return the clients */ public BlockingQueue<Client> getClients() { return clients; } /** * @param clients the clients to set */ public void setClients(BlockingQueue<Client> clients) { this.clients = clients; } /** * @return the isRunning */ public boolean isRunning() { return isRunning; } /** * @param isRunning the isRunning to set */ public void setRunning(boolean isRunning) { this.isRunning = isRunning; } }